package com.dm.controller;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSON;
import com.dm.entity.CompanyGroupInfoDO;
import com.dm.entity.CompanyInfoDO;
import com.dm.entity.DataInfoDO;
import com.dm.entity.SysUserDO;
import com.dm.query.QueryCompanyGroupInfo;
import com.dm.query.QueryCompanyInfo;
import com.dm.query.QueryDataInfo;
import com.dm.service.CompanyGroupInfoService;
import com.dm.service.CompanyInfoService;
import com.dm.service.DataInfoService;
import com.dm.service.SysUserService;
import com.dm.utils.RespsonseResult;


@Controller
@RequestMapping("company")
public class DataInfoController {
	
	private static Logger logger = LoggerFactory.getLogger(SysUserController.class);
	
	
	@Autowired
	DataInfoService dataInfoService ; 
	
	@Autowired
	CompanyGroupInfoService companyGroupInfoService ;
	
	@Autowired
	CompanyInfoService companyInfoService ;
	
	@Autowired
	SysUserService sysUserService ;
	
	@RequestMapping("selectDataInfo")
	public @ResponseBody Object selectDataInfo(HttpServletRequest request,QueryCompanyInfo queryCompanyInfo){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("排放信息查询:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isNotBlank(queryCompanyInfo.getCompanyName())){
				 queryCompanyInfo.setFuzzyCompanyName(queryCompanyInfo.getCompanyName());
				 queryCompanyInfo.setCompanyName(null);
			 }
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 queryCompanyInfo.setCompanyCode(userInfo.getCompanyInfo());
				 }else{
					 queryCompanyInfo.setGroupCode(userInfo.getCompanyInfo());
				 }
			 }
			 List< CompanyInfoDO >  data = companyInfoService.selectCompanyInfoList(queryCompanyInfo);
			 Map<String, Integer> dataKV = new HashMap<String, Integer>();
			 List<String> codeList = new ArrayList<String>();
			 if(data!=null && data.size()>0){
				 int i = 0 ;
				 for(CompanyInfoDO companyInfoDO : data){
					 codeList.add(companyInfoDO.getCompanyCode());
					 dataKV.put(companyInfoDO.getCompanyCode(), i);
					 i++;
				 }
				 List< DataInfoDO > dataInfos = dataInfoService.selectDataInfoBycodes(codeList);
				 for(DataInfoDO dataInfo : dataInfos){
					 CompanyInfoDO tmp = data.get(dataKV.get(dataInfo.getCcode()));
//					 tmp.setMethaneEmissions(dataInfo.getV01val());
//					 tmp.setTotalHydrocarbonEmissions(dataInfo.getV02val());
//					 tmp.setTotalHydrocarbonEmissions(dataInfo.getV02val());
//					 tmp.setNonTotalHydrocarbonEmissions(dataInfo.getV03val());
//					 tmp.setBenzeneEmissions(dataInfo.getV04val());
//					 tmp.setTolueneEmissions(dataInfo.getV04val());
//					 tmp.setXyleneEmissions(dataInfo.getV04val());
					 tmp.setDataInfo(dataInfo);
				 }
			 }
			 result.setData(data);
			 result.setMsg("查询成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	
	@RequestMapping("selectDataInfoHis")
	public @ResponseBody Object selectDataInfoHis(HttpServletRequest request,QueryCompanyInfo queryCompanyInfo){
		 SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("排放信息查询:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isNotBlank(queryCompanyInfo.getCompanyName())){
				 queryCompanyInfo.setFuzzyCompanyName(queryCompanyInfo.getCompanyName());
				 queryCompanyInfo.setCompanyName(null);
			 }
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 queryCompanyInfo.setCompanyCode(userInfo.getCompanyInfo());
				 }else{
					 queryCompanyInfo.setGroupCode(userInfo.getCompanyInfo());
				 }
			 }
			 List< CompanyInfoDO >  data = companyInfoService.selectCompanyInfoList(queryCompanyInfo);
			 Map<String, Integer> dataKV = new HashMap<String, Integer>();
			 List<String> codeList = new ArrayList<String>();
			 if(data!=null && data.size()>0){
				 int i = 0 ;
				 for(CompanyInfoDO companyInfoDO : data){
					 codeList.add(companyInfoDO.getCompanyCode());
					 dataKV.put(companyInfoDO.getCompanyCode(), i);
					 i++;
				 }
				 List< DataInfoDO > dataInfos = dataInfoService.selectDataInfoHisBycodes(codeList);
				 for(DataInfoDO dataInfo : dataInfos){
					 CompanyInfoDO tmp = data.get(dataKV.get(dataInfo.getCcode()));
//					 tmp.setMethaneEmissions(dataInfo.getV01val());
//					 tmp.setTotalHydrocarbonEmissions(dataInfo.getV02val());
//					 tmp.setTotalHydrocarbonEmissions(dataInfo.getV02val());
//					 tmp.setNonTotalHydrocarbonEmissions(dataInfo.getV03val());
//					 tmp.setBenzeneEmissions(dataInfo.getV04val());
//					 tmp.setTolueneEmissions(dataInfo.getV04val());
//					 tmp.setXyleneEmissions(dataInfo.getV04val());
					 tmp.setDataInfo(dataInfo);
				 }
			 }
			 result.setData(data);
			 result.setMsg("查询成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result);    
	}
	
	
	
	
	@RequestMapping("insertData")
	public @ResponseBody Object insertData(HttpServletRequest request,QueryCompanyInfo queryCompanyInfo){
		SysUserDO userInfo = (SysUserDO) request.getSession().getAttribute("user");
		 logger.info("排放信息查询:"+userInfo);
		 RespsonseResult result = new RespsonseResult();
		 try {
			 if(StringUtils.isNotBlank(queryCompanyInfo.getCompanyName())){
				 queryCompanyInfo.setFuzzyCompanyName(queryCompanyInfo.getCompanyName());
				 queryCompanyInfo.setCompanyName(null);
			 }
			 //判断用户权限
			 if(userInfo.getType() != 0){
				 if(userInfo.getType() >2){
					 queryCompanyInfo.setCompanyCode(userInfo.getCompanyInfo());
				 }else{
					 queryCompanyInfo.setGroupCode(userInfo.getCompanyInfo());
				 }
			 }
			 List< CompanyInfoDO >  data = companyInfoService.selectCompanyInfoList(queryCompanyInfo);
			 Map<String, Integer> dataKV = new HashMap<String, Integer>();
			 List<String> codeList = new ArrayList<String>();
			 if(data!=null && data.size()>0){
				 int i = 0 ;
				 for(CompanyInfoDO companyInfoDO : data){
					 codeList.add(companyInfoDO.getCompanyCode());
					 dataKV.put(companyInfoDO.getCompanyCode(), i);
					 i++;
				 }
				 List< DataInfoDO > dataInfos = dataInfoService.selectDataInfoBycodes(codeList);
				 for(DataInfoDO dataInfo : dataInfos){
					 dataInfoService.insertDataInfo(dataInfo);
				 }
			 }
			 result.setData(data);
			 result.setMsg("查询成功");
			 result.setCode("0");
		} catch (Exception e) {
			e.printStackTrace();
			result.setMsg("查询出错");
			result.setCode("1");
		}
		return JSON.toJSON(result); 
	}

}
